import requests
import re
from urllib.parse import unquote
import json
from pprint import pprint
from DrissionPage import ChromiumPage
import os

folder_path = "D:\\PycharmProject\\Douyin\\pic"

url = 'https://www.xiaohongshu.com/user/profile/5acc23bbe8ac2b6a248ab88b'
#url = 'https://www.xiaohongshu.com/explore/66d54679000000001d038c0f?xsec_token=AB3CZnj7bmWmt9oMJC7fyMW7GfJSxEu8MXlRKKdxhejd8=&xsec_source=pc_user'

headers = {
    'cookie':'abRequestId=736449df-7a56-5745-b964-135ea07f1ce0; xsecappid=xhs-pc-web; a1=19057f6577b6adtap20mksriq89zskocez5eagj1b50000117031; webId=865e5deaae925a394ca1c003bcc63797; gid=yj82WiK2iWd4yj82WiK2WukVWDK0f90UJ86T2yDMF3AYjd28uMTlS0888yyW8qy8DYSqifYD; acw_tc=616d2ca80f2c07fa4ea2ad4d5e7d7e2687bfec13b6ee9478bb11983d036e986b; websectiga=82e85efc5500b609ac1166aaf086ff8aa4261153a448ef0be5b17417e4512f28; sec_poison_id=26d1d6ec-7e26-4745-b4f0-658ccec5d8f0; web_session=0400698ef432c15677bb4701ed344b0c4f3bb1; webBuild=4.32.1',
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
}


response = requests.get(url, headers=headers)
# pprint(response.text)
html = response.text
info = re.findall('<script>window.__INITIAL_STATE__=(.*?)</script>',html)[0]
info_str_1 = re.findall('"user":(.*?),"pageScrolled"',info)
info_str_2 = '{'.join(info_str_1)
info_str_final = info_str_2+'}'
json_data = json.loads(info_str_final)
pprint(json_data)
notes = json_data['notes'][0]
for i in notes:
    note = i
    if note:
        note_id = note['id']
        index = str(note['index'])
        note_title = note['noteCard']['displayTitle']
        print(note_id,index,note_title)
        note_url = 'https://www.xiaohongshu.com/explore/' + note_id
        # print(note_url)
        note_response = requests.get(note_url, headers=headers)
        note_html = note_response.text
        note_info =re.findall('<meta name="og:image" content="(.*?)">',note_html)
        note_title = re.sub(r'\s+', '', note_title)
        dir_name = folder_path + '\\' + note_title
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        j = 0
        for i in note_info:
            pic_content = requests.get(i,headers=headers).content
            with open(dir_name + '\\' + str(j) + '.jpg', 'wb') as f:
                f.write(pic_content)
            j += 1